package com.jwl.idc.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

import com.jwl.idc.actionbean.UserBean;

public class LocalDBManager {

    // 插入用户资料
    public static Uri insertUserInfo(Context context, UserBean.User data) {
        ContentValues cv = new ContentValues();
        cv.put(ContactDBConstant.USER_INFO_UID, data.getUser().getCity());
        cv.put(ContactDBConstant.USER_INFO_LOGIN, data.getUser().getLogin());
        cv.put(ContactDBConstant.USER_INFO_NAME, data.getUser().getName());
        cv.put(ContactDBConstant.USER_INFO_PINYINNAME, data.getUser().getPinyinName());
        cv.put(ContactDBConstant.USER_INFO_SIGNATURE, data.getUser().getSignature());
        cv.put(ContactDBConstant.USER_INFO_PHONENUMBER, data.getUser().getPhoneNumber());
        cv.put(ContactDBConstant.USER_INFO_EMAIL, data.getUser().getEmail());
        cv.put(ContactDBConstant.USER_INFO_CITY, data.getUser().getCity());
        cv.put(ContactDBConstant.USER_INFO_GENDER, data.getUser().getGender());
        cv.put(ContactDBConstant.USER_INFO_PHOTOURL, data.getUser().getPhotoUrl());
        cv.put(ContactDBConstant.USER_INFO_TOKEN, data.getToken());
        cv.put(ContactDBConstant.USER_INFO_AUTO, data.getUser().getAutoLogin());
        Uri uri = context.getContentResolver().insert(
                ContactDBConstant.USERINFO_URI, cv);
        return uri;
    }

    // 删除个人资料
    public static int deleteUserInfo(Context context, String userId) {
        int result = context.getContentResolver().delete(
                ContactDBConstant.USERINFO_URI,
                ContactDBConstant.USER_INFO_UID + " = ? ",
                new String[]{userId});
        return result;
    }

    //修改个人资料
    public static int updateUserinfo(Context context, String userId,
                                     ContentValues cv) {
        int result = 0;
        result = context.getContentResolver().update(
                ContactDBConstant.USERINFO_URI, cv,
                ContactDBConstant.USER_INFO_UID + " = ? ",
                new String[]{userId});
        return result;
    }

    //查询个人资料
    public static int queryUserInfo(Context context, String userId) {
        int result = 0;
        Cursor cursor = context.getContentResolver().query(
                ContactDBConstant.USERINFO_URI, null,
                ContactDBConstant.USER_INFO_UID + " = ? ",
                new String[]{userId}, null);
        if (cursor.moveToFirst()) {
            result = 1;
        }
        cursor.close();
        return result;
    }

    //查询自动登录的用户
    public static UserBean.User queryUserInfo(Context context) {
        Cursor cursor = context.getContentResolver().query(
                ContactDBConstant.USERINFO_URI, null,
                ContactDBConstant.USER_INFO_AUTO + " = ? ",
                new String[]{"1"}, null);


        if (cursor.moveToFirst()) {
            UserBean.User data = new UserBean.User();

            data.getUser().setGender(cursor.getInt(cursor
                    .getColumnIndexOrThrow(ContactDBConstant.USER_INFO_GENDER)));
            data.getUser().setPhotoUrl(cursor.getString(cursor
                    .getColumnIndexOrThrow(ContactDBConstant.USER_INFO_PHOTOURL)));
            data.getUser().setCity(cursor.getString(cursor
                    .getColumnIndexOrThrow(ContactDBConstant.USER_INFO_CITY)));
            data.getUser().setName(cursor.getString(cursor
                    .getColumnIndexOrThrow(ContactDBConstant.USER_INFO_NAME)));
            data.getUser().setPhoneNumber(cursor.getString(cursor
                    .getColumnIndexOrThrow(ContactDBConstant.USER_INFO_PHONENUMBER)));
            data.getUser().setId(cursor.getString(cursor
                    .getColumnIndexOrThrow(ContactDBConstant.USER_INFO_UID)));
            data.getUser().setSignature(cursor.getString(cursor
                    .getColumnIndexOrThrow(ContactDBConstant.USER_INFO_SIGNATURE)));
            data.getUser().setEmail(cursor.getString(cursor
                    .getColumnIndexOrThrow(ContactDBConstant.USER_INFO_EMAIL)));
            data.setToken(cursor.getString(cursor
                    .getColumnIndexOrThrow(ContactDBConstant.USER_INFO_TOKEN)));
            data.getUser().setAutoLogin(cursor.getInt(cursor
                    .getColumnIndexOrThrow(ContactDBConstant.USER_INFO_AUTO)));
            data.getUser().setLogin(cursor.getString(cursor
                    .getColumnIndexOrThrow(ContactDBConstant.USER_INFO_LOGIN)));
            data.getUser().setPinyinName(cursor.getString(cursor
                    .getColumnIndexOrThrow(ContactDBConstant.USER_INFO_PINYINNAME)));
            data.getUser().setSignature(cursor.getString(cursor
                    .getColumnIndexOrThrow(ContactDBConstant.USER_INFO_SIGNATURE)));

            cursor.close();
            return data;
        }
        cursor.close();
        return null;
    }


}
